package com.yao.sys.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yao.common.bean.PageQueryBean;
import com.yao.common.helper.ValidateHelper;
import com.yao.common.resp.ApiResponse;
import com.yao.fin.service.FinancialTemplateService;
import com.yao.sys.entity.dto.UserDto;
import com.yao.sys.service.UserService;

import cn.hutool.core.lang.Console;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 用户表 Controller
 *
 * @author autoCode
 * @version 1.0.0-SNAPSHOT
 * @date 2020-03-23
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping(value = "/user")
public class UserController {

    private static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;
    @Autowired
    private FinancialTemplateService financialTemplateService;

    @ApiOperation(value = "新增用户", notes = "新增用户")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ApiResponse<Object> add(@RequestBody UserDto model) {

        ValidateHelper.validateNull(model, new String[] {"userName", "password", "sex", "idCard", "phone"});

        // 校验身份证号
        ValidateHelper.validateIdCard(model.getIdCard());

        userService.add(model);

        return ApiResponse.success();
    }

    @ApiOperation(value = "修改用户", notes = "修改用户")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ApiResponse<Object> update(@RequestBody UserDto model) {
        ValidateHelper.validateNull(model, new String[] {"id", "userName", "sex", "idCard", "phone"});

        return ApiResponse.success(userService.modifyById(model));
    }

    @ApiOperation(value = "删除用户", notes = "逻辑删除用户")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public ApiResponse<Object> deleteById(@PathVariable("id") Integer id) {

        ValidateHelper.validateId(id);

        return ApiResponse.success(userService.deleteById(id));
    }

    @ApiOperation(value = "查看用户详情", notes = "查看用户详情")
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    public ApiResponse<UserDto> detailById(@PathVariable("id") Integer id) {

        ValidateHelper.validateId(id);


        return ApiResponse.success(userService.byId(id));
    }



    @ApiOperation(value = "用户列表(分页)", notes = "用户列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ApiResponse<IPage<UserDto>> list(@RequestBody PageQueryBean queryBean) {

        Console.log(queryBean.getCurrent());

        IPage<UserDto> iPage = userService.listpage(queryBean);

        return ApiResponse.success(iPage);
    }

    @ApiOperation(value = "用户登陆", notes = "用户登陆")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ApiResponse<Object> login(UserDto model) {

        return ApiResponse.success(userService.login(model));
    }

}